package com.saic.data.main;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response.Status.Family;

import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.json.JSONObject;

import com.saic.data.table.TableAction;
import com.saic.data.table.TableConf;
import com.saic.data.table.TableFile;
import com.saic.data.table.TableUtil;
import com.saic.data.util.DBUtil;
import com.saic.data.util.FileUtil;
import com.saic.data.util.HbaseUtil;
import com.saic.data.util.ZookeeperUtil;

public class Test {
	
	public static void main(String[] args) throws Exception {
		
		HbaseUtil.deleteRowAll("sap2_csv_tmp");
		HbaseUtil.deleteRowAll("sap1_csv_tmp");
		//HbaseUtil.columnCopy(TableUtil.streamchecktab, TableUtil.streamcheckfamily, "streamNo","modifieddate" );
		//HbaseUtil.updateData(TableUtil.streamchecktab, "SAP1201607".getBytes(),TableUtil.streamcheckfamily, "modifieddate", "20160828194400");
		HbaseUtil.updateData(TableUtil.configtab, "20170330040008e4f275d85374fc1f6ddb051f48f61620".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.New.toString());
		HbaseUtil.updateData(TableUtil.configtab, "20170330060022385bd0d396c59034d000644f5a9d91c9".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.New.toString());
		HbaseUtil.updateData(TableUtil.configtab, "2017040304000948402bd42af7d174bf5e73d1127fcd6d".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.New.toString());
		HbaseUtil.updateData(TableUtil.configtab, "201704030600230acae195441810f9989f17fb069bb9eb".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.New.toString());
		HbaseUtil.updateData(TableUtil.configtab, "20170503150855d865d1850ecdf8b17fb5fdfcd62bed0a".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.New.toString());
		//HbaseUtil.formatColText("SH_SMCVDMS_SNW_OWINFO.TM_INF_FEEDBACK", "cf", "CORRECTIVE_ACTION");
		//HbaseUtil.columnReplace("clm.t_linkman", "cf", "lm_detail_apperceive","\r\n","\r");
		/*HbaseUtil.updateData(TableUtil.configtab, "2016082804020126aac452da36ebf7fd70cefb84487ecb".getBytes(), 
				TableUtil.configtabFamily, "fileStats", TableAction.UploadRecord.toString());*/
		//String string = HbaseUtil.QueryByKeyName("clm.t_linkman", "11e2bcd6974d2e2edb339bfb3c531a49".getBytes(), "cf", "lm_detail_apperceive");
		//System.out.println(string);
	}
	
	public static void getTableSQL(String filename) throws Exception {
		ZookeeperUtil zooUtil = new ZookeeperUtil();
		Map<String, String> map = zooUtil.getProperties(zooUtil.rootpath);
		zooUtil.close();
		ResultScanner rScanner = HbaseUtil.QueryByConditionValue(TableUtil.configtab,TableUtil.configtabFamily,
				"fileName", filename);
		Result res=rScanner.next();
		if(!res.isEmpty()) {
			TableFile tFile = new TableFile(res, map); 
			rScanner.close();
			rScanner = HbaseUtil.QueryByConditionValue(tFile.gettConf().getTmpHtbl(),tFile.gettConf().dataFamily,
					"fileName",tFile.getFileName());
			for(Result result :rScanner){
				System.out.println(tFile.getInsertSQL(result));
			}
		}
		
	}
	
	public static void changDoneToUploadRecord() throws Exception {
		ResultScanner rScanner = HbaseUtil.QueryByConditionValue(TableUtil.configtab,TableUtil.configtabFamily,
				"fileStats", TableAction.Done.toString());
		List<Put> pList = new ArrayList<Put>();
		for(Result result : rScanner){
		   Put put=new Put(result.getRow());
		   put.add(TableUtil.configtabFamily.getBytes(), "fileStats".getBytes(),TableAction.UploadRecord.toString().getBytes());
		   pList.add(put);
		}
		HbaseUtil.insertData(TableUtil.configtab, pList);	
	}
	public static void changUploadRecordToNew() throws Exception {
		ResultScanner rScanner = HbaseUtil.QueryByConditionValue(TableUtil.configtab,TableUtil.configtabFamily,
				"fileStats", TableAction.UploadRecord.toString());
		List<Put> pList = new ArrayList<Put>();
		for(Result result : rScanner){
		   Put put=new Put(result.getRow());
		   put.add(TableUtil.configtabFamily.getBytes(), "fileStats".getBytes(),TableAction.New.toString().getBytes());
		   pList.add(put);
		}
		HbaseUtil.insertData(TableUtil.configtab, pList);	
	}
	
	public static void ClearALLHbase() throws Exception {
		ZookeeperUtil zooUtil = new ZookeeperUtil();
		Map<String, String> map = zooUtil.getProperties(zooUtil.rootpath);
		zooUtil.close();
		for(String key:map.keySet()){
			TableConf tConf = new TableConf(key, map.get(key));
			HbaseUtil.deleteRowAll(tConf.getHistoryHtbl());
			HbaseUtil.deleteRowAll(tConf.getTmpHtbl());
		}
		HbaseUtil.deleteRowAll(TableUtil.streamchecktab);
		HbaseUtil.deleteRowAll(TableUtil.configtab);
	}



}
